<?php

$db = new models($host,$user,$pass,$db_name);

if(isset($_POST['request_edit'])){
	$id = trim($_POST['id']);
	$params = array(
		'tid'	=> 1,//empty(trim($_POST['answer_type'])) ? 1 : trim($_POST['answer_type']),
		'title'	=> trim($_POST['answer_name']),
		'answer'=> (bool)$_POST['answer_answer'],
		'qid'	=> empty(trim($_POST['answer_qid'])) ? 1 : trim($_POST['answer_qid'])
	);
	
	if($params['title']){
		
		$params['title'] = preg_replace("/[\/\'\"]/","",$params['title']);
		$db->select("id");
		$db->where("title='".$params['title']."' AND qid ='".$params['qid']."' AND id != '$id'");
		$answer = $db->get_data("answers");
		if(!$answer){
			$db->where("id='$id'");
			$db->update("answers",$params);
			redirect("$home_url/admin/answers/?notice=update_success");
		}else
			$error = "Câu trả lời đã có sẵn.";
		
	}else{
		$error = "Không được bỏ trống dữ liệu cập nhật.";
	}
}else{
	$id = trim($_GET['id']);
	//Get list question
	$db->select("id,name");
	$db->order("name");
	$list_questions = $db->get_data("question");
	
	$db->reset_data();
	$db->where("id='$id'");
	$answer = $db->get_data("answers");
	if(!$answer) redirect("$home_url/admin/answers/?notice=update_error");
}
?>
<div class="container" id="page-answer">
	<div class="row">
		<div class="col-sm-12 col-md-12 col-lg-12 text-center">
			<form role="form" class="form-horizontal" method="post" action="<?php echo $home_url; ?>/admin/answers/edit">
				<input type="hidden" name="id" value="<?php echo $id;?>" />
				<fieldset class="margin20">
					<legend class="text-center padding10-bottom">CHỈNH SỬA CÂU HỎI</legend>
					<?php
						if($error != ''){
							showAlert("alert-danger alert-error",$error);
						}
					?>
					<div class="form-group">
						<label for="answer_name" class="control-label col-xs-3">Câu trả lời</label>
						<div class="col-xs-6">
							<input type="text" name="answer_name" id="answer_name" class="form-control" value="<?php echo $answer[0]['title'];?>" size="40" placeholder="Nhập câu trả lời..." />
						</div>
					</div>
					<div class="form-group">
						<label for="answer_answer" class="control-label col-xs-3">Đáp án</label>
						<div class="col-xs-6">
							<select name="answer_answer" id="answer_answer" class="form-control">
								<option value="0" <?php if(!$answer[0]['answer']) echo "selected";?>>Sai</option>
								<option value="1" <?php if($answer[0]['answer'] == 1) echo "selected";?>>Đúng</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="answer_qid" class="control-label col-xs-3">Thuộc câu hỏi</label>
						<div class="col-xs-6">
							<select name="answer_qid" id="answer_qid" class="form-control">
							<?php
								if($list_questions != null){
									foreach($list_questions as $question){
										echo "<option value='".$question['id']."'>".$question['name']."</option>";
									}
								}else{
									echo "<option value=-1>Chưa có câu hỏi nào</option>";
								}
							?>
							</select>
						</div>
					</div>
					<div class="form-group">
						<div class="col-xs-offset-3 col-xs-6">
							<button type="submit" name="request_edit" id="request_edit" class="btn btn-primary">Cập nhật câu trả lời</button>
						</div>
					</div>
				</fieldset>
			</form>
		</div>
	</div>
</div>